/**
 *@auther: tang
 @Date: 2020-11-11
 *Project: group-booking-shop-manage-system
 */
import React from "react"
import routerMap from "../routerMap";
import {Route,Redirect} from "react-router-dom"
import Home from "../Layout/Home";
import Login from "../pages/Login";
import {message} from "antd";
import getOneConsArr from "../util/oneConsArr";
import {Observer,inject} from "mobx-react"
@inject("userStore","permissStore")
@Observer
class BasicRoutes extends React.Component {
    bindRoutes(){
        let isLogin = this.props.userStore.userInfo;
        // 先过滤再整合
        let routes =  (routerMap.filter(item=>{
            if (isLogin){
            //    如果登陆了，返回首页和404,替换首页为index
                if (item.name==="App"){
                    item.component = Home
                }
                return item.name==="Home" || item.name==="404" || item.name==="App"
            }else{
                if (item.name==="App"){
                    item.component = Login
                }
                return item.name==="Login" || item.name==="404" || item.name==="App"
            }
        })).map(item=>{
            if (item.name==="App"){
                return <Route exact key={item.menuId} path={item.path} component={item.component}/>
            }
            return <Route key={item.menuId} path={item.path} component={item.component}/>
        })

        return routes
    }
     bindRed(){
        let {userStore} = this.props;
        let target = this.props.location.pathname;
        let otherRoutes = getOneConsArr([...userStore.menuInfo?userStore.menuInfo:""])
       let  targetRoute = routerMap.find(item=>{
           return  item.path===target
        })
        let othRoute = {};
        if (otherRoutes){
            othRoute = otherRoutes.find(item=>{
                return item.pathname == target
            })
        }
        // console.log(target,targetRoute)
        let isLogin = this.props.userStore.userInfo;
        // console.log(isLogin,targetRoute,othRoute)
         console.log(targetRoute,othRoute)
        if (isLogin &&targetRoute&& (targetRoute.name==="Login" || targetRoute.name==="/")){
            return <Redirect to="/index"/>
        }else if (!isLogin && targetRoute&& targetRoute.auth===true ){
            return <Redirect to="/login"/>
        }else if (!targetRoute && !othRoute){
        }
    //    不需要重定向
    }
    render() {
        return (
            <div>
                {this.bindRoutes()}
                {this.bindRed()}
            </div>
        )
    }
}

export {BasicRoutes as default}
